package mySingleQueue;

import myStack.EmptyException;

//原理：先进先出
//思路：增加尾指针，采用尾插、头删分别实现[入队、出队]
public class MyQueue {
    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node head;
    public Node last;
    public int size;

    //boolean offer(E e) 入队列
    public boolean offer(int e) {
        Node node = new Node(e);
        if (head==null) {
            head = node;
            last = node;
        } else {
            last.next = node;
            last = node;
        }
        size++;
        return true;
    }

    //E poll() 出队列
    public int poll() {
        if (isEmpty()) {
            throw new EmptyException("队列为空!");
        }
        if (head.next==null) {
            last=null;
        }
        int ret = head.val;
        head = head.next;
        size--;
        return ret;
    }

    //peek() 获取队头元素
    public int peek() {
        if (isEmpty()) {
            throw new EmptyException("队列为空!");
        }
        return head.val;
    }

    //int size() 获取队列中有效元素个数
    public int getSize() {
        return size;
    }

    //boolean isEmpty() 检测队列是否为空
    public boolean isEmpty() {
        return size==0;
    }

}
